package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	for true {
		fmt.Print("请输入需要挑选文件的文件夹目录（输入'exit'退出）：")
		sixPath, _ := reader.ReadString('\n')
		sixPath = strings.TrimSpace(sixPath)

		if sixPath == "exit" {
			break
		}

		fmt.Print("请输入检测上一步文件夹内的文件是否存在的目标文件夹目录（输入'exit'退出）：")
		threePath, _ := reader.ReadString('\n')
		threePath = strings.TrimSpace(threePath)

		if threePath == "exit" {
			break
		}

		fmt.Print("请输入保存筛选出的不用文件的文件夹目录（输入'exit'退出）：")
		diffPath, _ := reader.ReadString('\n')
		diffPath = strings.TrimSpace(diffPath)

		if diffPath == "exit" {
			break
		}

		//sixPath := "C:\\Users\\admin\\Desktop\\wenjianfenlei\\6000"
		//threePath := "C:\\Users\\admin\\Desktop\\wenjianfenlei\\3000"
		//diffPath := "C:\\Users\\admin\\Desktop\\wenjianfenlei\\diff"
		do(sixPath, threePath, diffPath)
		fmt.Println("操作完成")
	}
}

func do(sixPath string, threePath string, diffPath string) {
	//6000文件文件名
	var sixPathFileName []string
	sixPathFileName, _ = getAllFileNames(sixPath, sixPathFileName)

	fmt.Println("sixPathFileName", sixPathFileName)

	//3000文件文件名
	var threePathFileName []string
	threePathFileName, _ = getAllFileNames(threePath, threePathFileName)

	fmt.Println("threePathFileName", threePathFileName)

	var diffPathFileName []string
	//获取6000目录下在3000目录下那个文件名不存在
	for _, value := range sixPathFileName {
		if !stringExistsInSlice(value, threePathFileName) {
			diffPathFileName = append(diffPathFileName, value)
		}
	}

	if len(diffPathFileName) > 0 {
		for _, value := range diffPathFileName {
			// 调用方法复制文件
			sourceDir := fmt.Sprintf("%s\\%s", sixPath, value)
			fmt.Println("复制", sourceDir, "到", diffPath)
			err := copyFileToDirectory(sourceDir, diffPath)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(sourceDir, "复制完成")
		}
	}

	fmt.Println("文件复制完成")
}

// 获取指定目录下所有文件的文件名并存入切片
func getAllFileNames(directory string, fileNames []string) ([]string, error) {
	err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		// 如果是文件而不是目录，则将文件名添加到切片中
		if !info.IsDir() {
			name := info.Name()
			trimmed := strings.TrimSpace(name)
			noTabs := strings.ReplaceAll(trimmed, "\t", "")
			fileNames = append(fileNames, noTabs)
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return fileNames, nil
}

// 判断字符串是否存在于切片中
func stringExistsInSlice(str string, slice []string) bool {
	for _, s := range slice {
		if s == str {
			return true
		}
	}
	return false
}

// 将指定文件复制到目标目录
func copyFileToDirectory(sourceFile, destinationDir string) error {
	// 获取源文件的名称
	sourceFileName := filepath.Base(sourceFile)

	// 构建目标文件的路径
	destinationFile := filepath.Join(destinationDir, sourceFileName)

	// 打开源文件
	source, err := os.Open(sourceFile)
	if err != nil {
		return err
	}
	defer source.Close()

	// 创建目标文件
	destination, err := os.Create(destinationFile)
	if err != nil {
		return err
	}
	defer destination.Close()

	// 复制文件内容
	_, err = io.Copy(destination, source)
	if err != nil {
		return err
	}

	return nil
}
